package com.example.client.redis;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

@Component("UserGenerator")
public class UserGenerator implements KeyGenerator {
    private String PREFIX = "CACH-USER-";
    @Override
    public Object generate(Object target, Method method, Object... params) {
        LocalDate now = LocalDate.now();
        String today = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        StringBuilder keyBuilder = new StringBuilder(PREFIX);
        keyBuilder.append(today+"-");
        keyBuilder.append(target.getClass().getSimpleName()).append("-");
        keyBuilder.append(method.getName()).append("-");
        if (params.length > 0) {
            keyBuilder.append(Arrays.deepHashCode(params));
        }
        return keyBuilder.toString();

    }
}
